package com.yimiyisu.contract_cust.controller;

import com.zen.ZenController;
import com.zen.ZenData;
import com.zen.ZenEngine;
import com.zen.ZenResult;
import com.zen.annotation.AccessRole;
import com.zen.annotation.Inject;
import com.zen.enums.ZenRole;

@AccessRole(ZenRole.ADMIN)
public class Invoice extends ZenController {
    @Inject
    private ZenEngine zenEngine;

    /**
     * 添加发票
     */
    public ZenResult addInvoice(ZenData data) {
        long amount = Long.parseLong(data.get("amount"));
        String contractId = data.get("related");
        ZenData contractData = ZenData.create("id",contractId);
        ZenResult contractResult = zenEngine.execute("get/contract", contractData);
        if("1".equals(data.get("type"))){
            long invoiceIssuance = contractResult.getLong("invoiceIssuance");
            contractData.put("invoiceIssuance",invoiceIssuance + amount);
            zenEngine.execute("patch/contract", contractData);
        }else{
            long invoiceReceipt = contractResult.getLong("invoiceReceipt");
            contractData.put("invoiceReceipt",invoiceReceipt + amount);
            zenEngine.execute("patch/contract", contractData);
        }
        zenEngine.execute("put/invoice", data);
        return ZenResult.success();
    }
}
